package com.paperobot.action;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.paperobot.common.util.PageUtil;
import com.paperobot.model.Magazine;
import com.paperobot.service.PaperobotService;

public class MagazineAction extends BaseAction{

	private static final long serialVersionUID = 4460081108803689207L;
	
	private PaperobotService paperobotService;;
	private Magazine magazine;
	private List<Magazine> magazineList = new ArrayList<Magazine>();
	private PageUtil pageUtil = new PageUtil();
	

	
	@SuppressWarnings("unchecked")
	public String listMagazine(){
		try {
			String pagetype = request.getParameter("pagetype");
			if(pagetype == null){
				pagetype = "first";
			}
			
			String currentPage = request.getParameter("currentPage");
			if(currentPage != null){
				pageUtil.setCurrentPage(Integer.parseInt(currentPage));
			}
			
			List<Magazine> allMagazineList = paperobotService.getMagazineList();
			magazineList = pageUtil.getCurrentPageRows(pagetype, allMagazineList.size(), allMagazineList);
			
			return SUCCESS;
			
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}

	public String updateMagazine(){
		try {
			String magazineId = request.getParameter("magazineId");
			magazine = paperobotService.getMagazine(Long.parseLong(magazineId));
			
			return "update";
			
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}

	public String deleteMagazine(){
		try {
			String magazineId = request.getParameter("magazineId");
			int rs = paperobotService.deleteMagazine(Long.parseLong(magazineId));
			if(rs > 0){
				return "delete";
			}
			return ERROR;
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}

	public String addMagazine(){

		 try {
			 String title = request.getParameter("title");
			 String type = request.getParameter("type");
			 String abstracts = request.getParameter("abstracts");
			 String content = request.getParameter("content");
			 String status = request.getParameter("status");

			 Magazine magazine = new Magazine();
			 magazine.setTitle(title);
			 magazine.setType(type);
			 magazine.setAbstracts(abstracts);
			 magazine.setContent(content);
			 magazine.setStatus(Integer.parseInt(status));
			 
			 
			 if(paperobotService.addMagazine(magazine) > 0){
			 	logger.info("添加期刊成功。");
			 	request.setAttribute("result", "添加期刊成功！");
			 	return SUCCESS;
			 }else{
				logger.info("添加期刊失败。");
			 	request.setAttribute("result", "添加期刊失败！");
			 	return ERROR;
			 }
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	
	}

	public String saveMagazine(){
		 try {
			 String magazineId = request.getParameter("magazineId");
			 String title = request.getParameter("title");
			 String type = request.getParameter("type");
			 String abstracts = request.getParameter("abstracts");
			 String content = request.getParameter("content");
			 String status = request.getParameter("status");

			 Magazine magazine = new Magazine();
			 magazine.setMagazineId(Long.parseLong(magazineId));
			 magazine.setTitle(title);
			 magazine.setType(type);
			 magazine.setAbstracts(abstracts);
			 magazine.setContent(content);
			 magazine.setStatus(Integer.parseInt(status));
			 
			 if(paperobotService.updateMagazine(magazine) > 0){
			 	logger.info("更新期刊成功。");
			 	request.setAttribute("result", "更新期刊成功！");
			 	return SUCCESS;
			 }else{
				logger.info("更新期刊失败。");
			 	request.setAttribute("result", "更新期刊失败！");
			 	return ERROR;
			 }
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}
	
	
	
	@Resource
	public void setPaperobotService(PaperobotService paperobotService) {
		this.paperobotService = paperobotService;
	}
	public PageUtil getPageUtil() {
		return pageUtil;
	}
	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}
	public Magazine getMagazine() {
		return magazine;
	}
	public void setMagazine(Magazine magazine) {
		this.magazine = magazine;
	}
	public List<Magazine> getMagazineList() {
		return magazineList;
	}
	public void setMagazineList(List<Magazine> magazineList) {
		this.magazineList = magazineList;
	}
	
	
	
	
	
	
}
